package com.faxsun.service.handlers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.cms.file.domain.StaticAsset;
import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.openadmin.dto.CriteriaTransferObject;
import org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria;
import org.broadleafcommerce.openadmin.dto.PersistencePackage;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerEventHandlerAdapter;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerEventHandlerResponse;

/**
 * 
 * 图片资源
 * @author zhaoteng.song@faxsun.com
 *
 */
public class AssetsHandler extends PersistenceManagerEventHandlerAdapter {

	@SuppressWarnings("unused")
	private static final Log LOG = LogFactory.getLog(AssetsHandler.class);

	@Override
	public PersistenceManagerEventHandlerResponse preFetch(
			PersistenceManager persistenceManager,
			PersistencePackage persistencePackage, CriteriaTransferObject cto)
			throws ServiceException {

		boolean isAssets = persistencePackage.getCeilingEntityFullyQualifiedClassname().equals(StaticAsset.class.getName());
		if (isAssets) {
			//图片资源类型的读取
			//设置按更新时间从近到远排序
			FilterAndSortCriteria sortCriteria = new FilterAndSortCriteria("auditable.dateUpdated");
			sortCriteria.setSortAscending(false);
			cto.add(sortCriteria);
			return new PersistenceManagerEventHandlerResponse()
			.withStatus(PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED);
		}
		return new PersistenceManagerEventHandlerResponse()
				.withStatus(PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.NOT_HANDLED);
	}

}
